/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.delivery.restclient;

import com.mycompany.delivery.dto.ContactDTO;
import com.mycompany.delivery.dto.DTOState;
import com.mycompany.delivery.dto.DeliveryDTO;
import com.mycompany.delivery.dto.UserDTO;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author mhunek
 */
public class Test {

    public static void main(String args[]) {
        Map<String, String> vars = new HashMap<String, String>();
        vars.put("id", "5");
           vars.put("id2", "4");
              vars.put("id3", "3");
                      vars.put("id1", "1");
                          vars.put("id37" , "37");
                                vars.put("id45", "45");
        RestTemplate restTemplate = new RestTemplate();
        ContactDTO c = restTemplate.getForObject("http://localhost:8080/pa165/contacts/getContactJson/{id1}", ContactDTO.class, vars);
        System.out.println(c);
        
        
        DeliveryDTO d = restTemplate.getForObject("http://localhost:8080/pa165/deliveries/getDeliveryJson/1", DeliveryDTO.class);
        System.out.println(d);
        
        d.setPrice(419);
        

        ContactDTO createdC = new ContactDTO(null, "test", "test", "mass@seff.cz", "test", "test", "test", "69532847");
        try {
            c = restTemplate.postForObject("http://localhost:8080/pa165/contacts/update", createdC, ContactDTO.class);
            System.out.println(c);
            
            d = restTemplate.postForObject("http://localhost:8080/pa165/deliveries/update", d, DeliveryDTO.class);
            System.out.println("Update succesful: " + d);
        } catch (HttpClientErrorException e) {
          
            System.err.println(e.getMessage());

        }
        try {
            ContactDTO[] contacts = restTemplate.getForObject("http://localhost:8080/pa165/contacts/getAllContactJson", ContactDTO[].class);
            for (ContactDTO cont : contacts) {
                System.out.println(cont);
            }
        } catch (HttpClientErrorException e) {
            /**
             *
             * If we get a HTTP Exception display the error message
             */

            System.err.println(e.getMessage());

        }
         ContactDTO c3 = restTemplate.getForObject("http://localhost:8080/pa165/contacts/getContactJson/{id2}", ContactDTO.class, vars);
          ContactDTO c2 = restTemplate.getForObject("http://localhost:8080/pa165/contacts/getContactJson/{id37}", ContactDTO.class, vars);
          ContactDTO c4 = restTemplate.getForObject("http://localhost:8080/pa165/contacts/getContactJson/{id45}", ContactDTO.class, vars); 
          
          System.out.println(c2);
          System.out.println(c3);          
          DeliveryDTO d2 = new DeliveryDTO(DTOState.DELIVERED, c2, c3, new Date(), new Date(), "note", 100, true, null, null);
          d2 = restTemplate.postForObject("http://localhost:8080/pa165/deliveries/update", d2, DeliveryDTO.class);
          System.out.print("Update succesful" + d2);
          
          c3.setCity("Updated city");
          c3 = restTemplate.postForObject("http://localhost:8080/pa165/contacts/update", c3, ContactDTO.class);
  
          DeliveryDTO d3 = restTemplate.getForObject("http://localhost:8080/pa165/deliveries/getDeliveryJson/{id37}", DeliveryDTO.class, vars);
          
          UserDTO [] couriers = restTemplate.getForObject("http://localhost:8080/pa165/deliveries/getCouriersJson", UserDTO[].class);
          UserDTO courier = couriers[1];
          //DeliveryDTO del = restTemplate.postForObject("http://localhost:8080/pa165/deliveries/addDeliveryToCourier", d3, DeliveryDTO.class, courier,  UserDTO.class);
          DeliveryDTO del = restTemplate.postForObject("http://localhost:8080/pa165/deliveries/addDeliveryToCourier/{id1}", d3, DeliveryDTO.class, vars);
          System.out.println("Id of curier: " + del.getCourier());
          //restTemplate.postForObject("http://localhost:8080/pa165/contacts/delete", c4, ContactDTO.class);
          //restTemplate.delete("http://localhost:8080/pa165/contacts/delete/{id37}", vars);
    }
}
